# 
# To change this template, choose Tools | Templates
# and open the template in the editor.
 
require 'csv'
require 'date'

def csv_to_array(file_location)
  if $csv == nil
    $csv = CSV::parse(File.open(file_location, 'r') {|f| f.read })
  end
  
  return $csv
end


module PNEngine
  class CsvLogger
    @@logfilename = "log/mininglog.csv"
    
    def self.get_csv
      return csv_to_array(@@logfilename)
    end
    
    def self.log_transition(action, time, user, task_id, task_name, op, case_id, net_id)
      file = File.open(@@logfilename , File::WRONLY|File::APPEND|File::CREAT)      
      file.write([action,time,user,task_id, task_name, op, case_id, net_id].join(",")+"\n")
      file.close
      
      if $csv == nil
        $csv = csv_to_array(@@logfilename)
      else
        $csv << [action,time,user,task_id, task_name, op, case_id, net_id]      
      end
    end
    
    def self.get_task_attractivity(task_name)
      csv = csv_to_array(@@logfilename)
      
      good = csv.select{|line| line[4] == task_name }.size
      all = csv.size
      return 2.5  * good/all
    end
    
    def self.get_task_timeperiod(task_name, action, case_id)
      csv = csv_to_array(@@logfilename)
      
      good = csv.select{|line| line[4] == task_name && line[5] == action && line[6].to_i == case_id}
      
      if !good.first.nil?
        good = good.first[1] unless good.first.nil?
        return (Time.new - Time.parse(good)).to_f        
      else
        return 0.0
      end
    end
    
    def self.get_delegation_ratio(task_name)
      csv = csv_to_array(@@logfilename)
      
      all = csv.select{|line| line[4] == task_name}.collect { |line| line[6] }.uniq
      good = csv.select{|line| line[4] == task_name && line[5] == 'delegate'}.collect{|line| line[6]}.uniq
      
      if 0 < all.size
        return 1.0 * good.size / all.size
      else
        return 0.0
      end
    end
    
    def self.get_adhoc_order(case_id)
      csv = csv_to_array(@@logfilename)
      
      start = csv.select { |entry| entry[4].index("transition_ad-hoc_start_") == 0 && entry[6].to_i == case_id.to_i && entry[0] == "automatic_fire"}.last
      ende = csv.select { |entry| (entry[4].index("transition_ad-hoc_end_") == 0 or entry[4].index("transition_ad-hoc_defaultEnd_") == 0) && entry[0] == "automatic_fire" && entry[6].to_i == case_id.to_i}.last

      if !start.nil? && !ende.nil?
        start_i = csv.index(start)
        ende_i = csv.index(ende)
        
        interesting = csv[start_i .. ende_i].
          select { |entry| entry[6].to_i == case_id.to_i && entry[0] == "manual_fire"}.
          collect { |entry| entry[4] }.uniq
        return interesting.join(",")
      end
      return []
    end
    
    def self.get_transition_count_for_case(case_id)
      csv = csv_to_array(@@logfilename)
      count = csv.select{ |entry| entry[6].to_i == case_id.to_i }.collect { |e| e[3] }.uniq.size
      return count
    end

    # selects cases runtimes from mininglog
    def self.get_time_period_for_case(case_id)
      csv = csv_to_array(@@logfilename)
      
      times = csv.select{ |entry| entry[6].to_i == case_id.to_i }.collect { |e| Time.parse(e[1]).to_f}.sort
      
      completed = true
      PNEngine::Transition.find(:all, :order => "id ASC").select{ |transition| transition.active? }.each do |transition|
        transition.active_combinations.each do |combination|
          if combination.select { |token| !combination.first.nil? && combination.first.case.id == case_id.to_i }.size > 0
            completed = false
          end
        end
      end
      
      start = times.first
      
      if start.nil?
        return 0.0
      end
      if !completed 
        ende = Time.new.to_f
      else
        ende = times.last
      end
      
      return (ende-start)
    end
    
    def self.get_involved_users_count_for_case(case_id)
      csv = csv_to_array(@@logfilename)
      count = csv.select{ |entry| entry[6].to_i == case_id.to_i && !entry[2].nil?}.collect { |e| e[2] }.uniq.size
      return count
    end
    
  end
end
